package cn.samir.sudoku.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.adapter.HistoryAdapter;
import cn.samir.sudoku.core.Sudokus;
import cn.samir.sudoku.db.dao.SudokuDao;
import cn.samir.sudoku.keys.Conf;
import cn.samir.sudoku.net.NetWorkHelper;
import cn.samir.sudoku.util.SharePopWindow;
import cn.samir.sudoku.util.ThreadPoolManager;
import cn.samir.sudoku.util.Utils;
import cn.samir.sudoku.view.LoadingDialog;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
//是每次刷新多少个
// 

public class UserHistoryActivity extends BaseActivity implements
		OnClickListener, OnRefreshListener<ListView>, OnItemClickListener {

	ImageView mHeadImageView;
	TextView mUserNameTextView;
	TextView mExpirenceTv;
	SharePopWindow sharePopWindow;
	View parentView;
	private ArrayList<Sudokus> mListItems;
	HistoryAdapter historyAdapter;
	ListView actualListView;
	PullToRefreshListView mPullRefreshListView;
	LoadingDialog loadingDialog;
	Activity mContext = this;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				File file = new File(msg.obj + "");
				if (file.exists()) {
					decodeBitmap(file);
				}
				break;
			case 2:
				if (msg.obj instanceof Bitmap) {
					mHeadImageView.setImageBitmap((Bitmap) msg.obj);
				} else {
					unregister();
				}
				break;

			case 3:
				// Call onRefreshComplete when the list has been refreshed.
				mPullRefreshListView.onRefreshComplete();
				break;
			case 5:
				loadingDialog.dismiss();
				break;
			case 16:
				SudokuDao dao = new SudokuDao(mContext);
				String code = Utils.getSharePreString(mContext, "code");
				ArrayList<Sudokus> sdk = dao.queryPassed(code);
				int totalscore = 0;
				for (Sudokus sudokus : sdk) {
					totalscore += Utils.getSocre(sudokus.score);
				}
				mExpirenceTv.setText("总得分：" + totalscore);
				mListItems.clear();
				mListItems.addAll(sdk);
				historyAdapter.clear();
				historyAdapter.appendToList(mListItems);
				loadingDialog.dismiss();
				mPullRefreshListView.onRefreshComplete();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		parentView = View.inflate(this, R.layout.activity_userhistory, null);
		setContentView(parentView);
		initViews();

	}

	@Override
	protected void onResume() {
		super.onResume();
		sharePopWindow = new SharePopWindow(this);
	}

	private void initViews() {
		loadingDialog = new LoadingDialog(this);
		mHeadImageView = getView(R.id.circle_usericon);
		mUserNameTextView = getView(R.id.tv_username);
		mExpirenceTv = getView(R.id.tv_expirence);
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		actualListView = mPullRefreshListView.getRefreshableView();
		mPullRefreshListView.setOnRefreshListener(this);
		actualListView.setOnItemClickListener(this);
		initUserInfos();
		initUserInfoDisplay();

	}

	private void initUserInfos() {
		Utils.getHistoryFromServer(this, handler);
		mListItems = new ArrayList<Sudokus>();
		historyAdapter = new HistoryAdapter(this);
		actualListView.setAdapter(historyAdapter);
	}

	private void initUserInfoDisplay() {
		String username = Utils.getUserName(this);
		if (username != null) {
			mUserNameTextView.setText(username);
			ImageLoader.getInstance().displayImage(
					Utils.getUserBigHeaderUrl(mContext), mHeadImageView);
			handler.sendEmptyMessage(16);
		}else{
			unregister();
		}

	}

	public void unregister() {
		mHeadImageView.setImageResource(R.drawable.login);
		mHeadImageView.setOnClickListener(this);
		mUserNameTextView.setText("未登录");
		mExpirenceTv.setText("经验：0/100");
	}

	public void decodeBitmap(final File file) {
		ThreadPoolManager.getInstance().addTask(new Runnable() {

			@Override
			public void run() {
				Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
				Message message = Message.obtain();
				message.what = 2;
				message.obj = bitmap;
				handler.sendMessage(message);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.circle_usericon:
			loginQQ();
			break;

		default:
			break;
		}

	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadingDialog.show("正在更新..");
		String label = DateUtils.formatDateTime(getApplicationContext(),
				System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL);
		// Update the LastUpdatedLabel
		refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
		Utils.getHistoryFromServer(this, handler);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Sudokus sudokus = (Sudokus) parent.getItemAtPosition(position);

	}
}
